<?php
namespace app\index\controller;

use app\common\controller\Base;
use app\index\model\Server;
use app\index\model\Retention as RetentionModel;
use think\Config;
use think\Request;

/**
 * 留存日志
 */
class Retention extends Base
{

    use \app\index\controller\traits\CurlRequest;

    protected $model;

    /**
     * 控制器初始化
     * @return   void
     */
    public function _initialize()
    {
        //基类授权
        //
        parent::_initialize();
        $this->model = new RetentionModel();

        # 记录开服日期的服务器
        # 
        $server = Server::getByServerId(1);
        $this->new_open_date = substr($server->open_date, 0, 10);
    }

    public function doList(Request $request)
    {

        //输入

        $sid = input('sid/s');
        $channel = input('channel/s');

        $startdate = input('startdate/s');
        $enddate = input('enddate/s');

        $map = [];

        //必选区间
        if (!empty($startdate) && !empty($enddate)) {
            $map = ['date' => array('between', array($startdate, $enddate))];
        } else if (!empty($startdate)) {
            $map = ['date' => array('>=', $startdate)];
        } else if (!empty($enddate)) {
            $map = ['date' => array('<=', $enddate)];
        } else {

            //默认参数配合仓库自动加载
            //
            $startdate = $this->new_open_date;
            $enddate = date('Y-m-d');
            $map = ['date' => array('between', array($startdate, $enddate))];
        }

        //可选统计
        if (!empty($sid)) {
            $map['server_id'] = $sid;
        }

        if (!empty($channel)) {
            $map['channel_id'] = $channel;
        }

        $res = $this->model->doList($map);
        _callback($res);
    }

    public function doClearTblFromNewOpen($opentime)
    {

        //闭包删除
        RetentionModel::destroy(function ($query) {
            $query->where('date', '>=', $opentime);
        });
    }
}
